/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.datalayer.daoimpl;

import cz.fi.muni.diplomka.common.enumeration.ResearchType;
import cz.muni.fi.diplomka.datalayer.dao.ResearchInProgressDao;
import cz.muni.fi.diplomka.datalayer.entity.ResearchInProgressEntity;
import cz.muni.fi.diplomka.datalayer.util.DbConnector;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jakub Kutil
 */
public class ResearchInProgressDaoImpl implements ResearchInProgressDao {

    private Connection con = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;

    public ResearchInProgressDaoImpl() {
    }

    @Override
    public void addResearchInProgress(ResearchInProgressEntity researchInProgressEntity) {
        if (researchInProgressEntity == null) {
            throw new IllegalArgumentException("ResearchInProgressEntity can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String insert = "INSERT INTO research_in_progress"
                    + "(player_id, name, time_of_finish) VALUES"
                    + "(?,?,?)";
            ps = con.prepareStatement(insert);


            ps.setLong(1, researchInProgressEntity.getPlayerId());
            ps.setString(2, researchInProgressEntity.getResearchType().toString());
            ps.setTimestamp(3, researchInProgressEntity.getTimeOfFinish());

            ps.executeUpdate();

        } catch (SQLException ex) {
            Logger.getLogger(ResearchInProgressDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public void editResearchInProgress(ResearchInProgressEntity researchInProgressEntity) {
        if (researchInProgressEntity == null) {
            throw new IllegalArgumentException("ResearchInProgressEntity can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String update = "UPDATE research_in_progress SET time_of_finish=? WHERE  city_id=? AND name=?";

            ps = con.prepareStatement(update);

            ps.setTimestamp(1, researchInProgressEntity.getTimeOfFinish());
            ps.setLong(2, researchInProgressEntity.getPlayerId());
            ps.setString(3, researchInProgressEntity.getResearchType().toString());

            ps.executeUpdate();
            
        } catch (SQLException ex) {
            Logger.getLogger(ResearchInProgressDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public void deleteResearchInProgress(Long id, ResearchType researchType) {
        if (id == null) {
            throw new IllegalArgumentException("ID can not be null");
        }
        if (researchType == null) {
            throw new IllegalArgumentException("ResearchType can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String delete = "DELETE FROM research_in_progress WHERE city_id=? AND name=?";

            ps = con.prepareStatement(delete);

            ps.setLong(1, id);
            ps.setString(2, researchType.toString());

            ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ResearchInProgressDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public List<ResearchInProgressEntity> getAllResearchsInProgressForPlayer(Long playerId) {
        if(playerId == null){
            throw new IllegalArgumentException("Player Id can not be null");
        }
        List<ResearchInProgressEntity> getAllResearchInProgressList = new ArrayList<>();

        try {
            con = DbConnector.getDBConnection();
            String getAllResearchsInProgressForPlayer = "SELECT * FROM research_in_progress WHERE player_id=?";

            ps = con.prepareStatement(getAllResearchsInProgressForPlayer);
            ps.setLong(1, playerId);

            rs = ps.executeQuery();

            while (rs.next()) {
                ResearchInProgressEntity researchInProgressEntity = new ResearchInProgressEntity();
                researchInProgressEntity.setPlayerId(rs.getLong("player_id"));
                researchInProgressEntity.setResearchType(ResearchType.valueOf(rs.getString("name")));
                researchInProgressEntity.setTimeOfFinish(rs.getTimestamp("time_of_finish"));

                getAllResearchInProgressList.add(researchInProgressEntity);

            }
        } catch (SQLException ex) {
            Logger.getLogger(ResearchInProgressDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return getAllResearchInProgressList;
    }
    
    @Override
    public List<ResearchInProgressEntity> getAllResearchsInProgress() {

        List<ResearchInProgressEntity> getAllResearchInProgressList = new ArrayList<ResearchInProgressEntity>();

        try {
            con = DbConnector.getDBConnection();
            String getAllResearchsInProgressForPlayer = "SELECT * FROM research_in_progress";

            ps = con.prepareStatement(getAllResearchsInProgressForPlayer);

            rs = ps.executeQuery();

            while (rs.next()) {
                ResearchInProgressEntity researchInProgressEntity = new ResearchInProgressEntity();
                researchInProgressEntity.setPlayerId(rs.getLong("player_id"));
                researchInProgressEntity.setResearchType(ResearchType.valueOf(rs.getString("name")));
                researchInProgressEntity.setTimeOfFinish(rs.getTimestamp("time_of_finish"));

                getAllResearchInProgressList.add(researchInProgressEntity);

            }
        } catch (SQLException ex) {
            Logger.getLogger(ResearchInProgressDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return getAllResearchInProgressList;
    }
}
